<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/18
 * Time: 21:38
 */

namespace app\storage\controller;

use app\storage\model\SeatModel;
use app\storage\model\ShelfTypeModel;
use app\storage\model\ShelvesModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;

class ShelfController extends AdminBaseController
{
    private $shelvesModel;
    use \app\storage\util\Common;

    public function __construct(ShelvesModel $shelvesModel,Request $request = null)
    {
        parent::__construct($request);
        $this->shelvesModel = $shelvesModel;
    }

    public function index()
    {
        $assign = $this->getAssignStorage();
        $assign['history'] = input('history');
        $this->assign($assign);
        return $this->fetch('index');
    }

    public function add()
    {
        $shelfTypeModel = new ShelfTypeModel();
        $assign['shelfType'] = $shelfTypeModel->select();
        $assign['storageInfo'] = $this->getAssignStorage()['storageInfo'];
        $this->assign($assign);
        return $this->fetch('add');
    }

    public function getShelf()
    {
        $where = $this->getWhere();
        $result = $this->shelvesModel->getShelf($where);
        return json($result);
    }

    public function addShelf()
    {
        $data = input();
        $data['uuid'] = create_uuid();
        $data['shelves_code'] = $data['type'] . str_pad($data['shelvesCode'], 2, '0', STR_PAD_LEFT);
        $data['seat_count'] = $data['floor'] * $data['floorCount'];
        $data['floor_count'] = $data['floorCount'];
        unset($data['shelvesCode'],$data['floorCount']);
        $direction = 'L';
        $halfCount = $data['floor_count'] / 2;
        for ($n = 0; $n < $data['floor']; $n++) {
            $j = 0;
            for ($i = 0; $i < $data['floor_count']; $i++) {
                $seat[] = [
                    'uuid' => create_uuid(),
                    'seat_code' => $data['shelves_code'] . $direction . ($n + 1) . str_pad($j++, 3, '0', STR_PAD_LEFT),
                    'status' => '0',
                    'shelves_uuid' => $data["uuid"],
                    'storage_uuid' => $data["storage_uuid"]
                ];
                if ($data['isDouble'] == 1 && $i >= $halfCount) {
                    $direction = 'R';
                    $i == ($halfCount - 1) ? $j = 0 : '';
                }
            }
        }
        $addSeat = true;
        Db::startTrans();
        if (!empty($seat)) {
            $addSeat = Db::table('iwh_seat')->insertAll($seat);
        }
        unset($data['name']);
        unset($data['isDouble']);
        $addShelf = Db::table('iwh_shelves')->insert($data);
        if ($addSeat == true && $addShelf == true) {
            Db::commit();
            $this->redirect('/storage/Shelf/index', 'storageUuid=' . $data['storage_uuid']);
        } else {
            Db::rollback();
            $this->error('添加异常');
        }
    }

    public function deleteShelf()
    {
        $uuid = input('uuid');
        $seatModel = new SeatModel();
        $reels = $seatModel->where(['shelves_uuid' => $uuid, 'is_used' => 1])->select()->toArray();
        $msg = empty($reels) ? '' : '请先清空货架下的物料';
        if (empty($reels)) {
            $result = $this->shelvesModel->deleteShelf($uuid);
            $msg = $result == false ? '删除失败' : '';
        }
        ajax_response(!boolval($msg), $msg);
    }

    public function getMaxCode()
    {
        $where = [
            'storage_uuid' => input('storageUuid'),
            'type' => input('type'),
        ];
        $count = $this->shelvesModel->where($where)->order('shelves_code desc')->value('substring(shelves_code,2)');
        ajax_return($count + 1);
    }

    public function judeShelfCode()
    {
        $shelveCode = input('code');
        if ($shelveCode > 99) {
            ajax_response(false, '超过最大可生成货架号');
        }
        $shelveCode = input('type') . str_pad($shelveCode, 2, '0', STR_PAD_LEFT);
        $where = [
            'storage_uuid' => input('storageUuid'),
            'shelves_code' => $shelveCode,
        ];
        $result = $this->shelvesModel->where($where)->find();
        if (!empty($result)) {
            ajax_response(false, '该货架号已存在请修改');
        }
        ajax_response(true);
    }
}